<template>
      <div class="commentItem">
        <div class="top">
            <div class="left">
                <span>{{obj.user.nickname}}</span> &nbsp;&nbsp;&nbsp;
                <span>2分钟前</span>
            </div>
            <span @click="replyFn(obj.id)">回复</span>
        </div>
        <!-- 第2层嵌套 star -->
        <!-- <div class="commentItem">
            <div class="top">
                <div class="left">
                    <span>{{obj.user.nickname}}</span> &nbsp;&nbsp;&nbsp;
                    <span>2分钟前</span>
                </div>
                <span>回复</span>
            </div>
            <div class="bottom">{{obj.content}}</div>
        </div> -->
        <!-- 
            递归组件：自己调用自己的一个组件就称为递归组件（注意：必须要有一个终止条件）
         -->
        <commentItem @reply="replyFn" 
        v-if="obj.parent" 
        :obj="obj.parent"></commentItem> 
        <!-- 第2层嵌套 end -->
        <div class="bottom">{{obj.content}}</div>
    </div>
</template>

<script>
export default {
    name: "commentItem", // 添加了name属性，就相当于注册了自身这个组件了
    props: {
        obj: {
            type: Object,
            required: true
        }
    },
    methods: {
        // 回复
        replyFn(id){
            // 子传父，把id传递给父组件
            this.$emit("reply", id)
        }
    }
}
</script>

<style lang="less" scoped>
.commentItem {
  border: 1px solid #ccc;
  padding: 5px;
  margin-top: 10px;
  .top {
    font-size: 12px;
    color: #aaa;
    display: flex;
    justify-content: space-between;
  }
  .bottom {
    font-size: 13px;
    line-height: 40px;
  }
}
</style>